home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / TupleDatabase / DiskLogTests.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  4.3 KB  |  192 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        DiskLogTests.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef    debug
  15. #define    debug    // we are always in debug mode when running tests
  16. #endif
  17.  
  18. #ifndef    __DISKLOG__
  19. #include "DiskLog.h"
  20. #endif
  21.  
  22. #ifndef    __DEBUGASSERT__
  23. #include "DebugAssert.h"
  24. #endif
  25.  
  26. #ifndef    __TUPLETESTS__
  27. #include "TupleTests.h"
  28. #endif
  29.  
  30. #ifndef    __STDLIB__
  31. #include "StdLib.h"
  32. #endif
  33.  
  34. #ifndef    __TIME_H
  35. #include "Time.h"
  36. #endif
  37.  
  38. #ifndef    __DEBUGGINGGEAR__
  39. #include "DebuggingGear.h"
  40. #endif
  41.  
  42. #ifndef    __DEBUGCONSTANTS__
  43. #include "DebugConstants.h"
  44. #endif
  45.  
  46. #ifndef    __FILESPEC__
  47. #include "FileSpec.h"
  48. #endif
  49.  
  50. #ifndef    __THREAD_H
  51. // #include "Thread.h"
  52. #endif
  53.  
  54. #pragma segment DiskLogTests
  55.  
  56. /***********************************|****************************************/
  57.  
  58. Boolean
  59. EnumerateTest ( TDiskLog& log )
  60. {
  61.     TDiskLogEnumerator enumerator ( log );
  62.  
  63.     unsigned long count = log.CountEntries ();
  64.     ASSERT_RETURN_ZERO ( count == enumerator.CountEntries () );
  65.     
  66.     for ( unsigned long i = 1; i <= count; i++ )
  67.     {
  68.         TYield ();
  69.         unsigned long index = ( (unsigned long) rand () % count ) + 1;
  70.         const TLogEntry* entry = enumerator.GetEntryByIndex ( index ); 
  71.         
  72.         if ( entry == nil )
  73.         {
  74.             chris << "### Error: entry #" << i << " was not gotten!" << flush;
  75.             ASSERT_RETURN_ZERO ( entry != nil );
  76.         }
  77.         else if ( chrisFlag.Flag ( kExtensiveTupleDBDescribe ) )
  78.         {
  79.             chris << "#" << i << ": " << *entry << endl;
  80.         }
  81.     }
  82.     
  83.     return true;
  84. }
  85.  
  86. /***********************************|****************************************/
  87.  
  88. Boolean
  89. IterateTest ( TDiskLog& log )
  90. {
  91.     TDiskLogIterator iterator ( log );
  92.     unsigned long expectedCount = log.CountEntries (), actualCount;
  93.     const TLogEntry* entry;
  94.     
  95.     actualCount = 0;
  96.     for ( entry = iterator.GetFirstEntry ();
  97.           entry != nil;
  98.           entry = iterator.GetNextEntry () )
  99.     {
  100.         TYield ();
  101.         ASSERT_RETURN_ZERO ( entry != nil );
  102.         actualCount++;
  103.  
  104.         if ( chrisFlag.Flag ( kExtensiveTupleDBDescribe ) )
  105.             chris << *entry << endl;
  106.     }
  107.  
  108.     ASSERT_RETURN_ZERO ( expectedCount == actualCount );
  109.     
  110.     actualCount = 0;
  111.     for ( entry = iterator.GetLastEntry ();
  112.           entry != nil;
  113.           entry = iterator.GetPreviousEntry () )
  114.     {
  115.         ASSERT_RETURN_ZERO ( entry != nil );
  116.         actualCount++;
  117.  
  118.         if ( chrisFlag.Flag ( kExtensiveTupleDBDescribe ) )
  119.             chris << *entry << endl;
  120.     }
  121.  
  122.     ASSERT_RETURN_ZERO ( expectedCount == actualCount );
  123.     
  124.     return true;
  125. }
  126.  
  127. /***********************************|****************************************/
  128.  
  129. Boolean
  130. EnumIterateTests ( TDiskLog& log )
  131. {
  132.     ASSERT_RETURN_ZERO ( EnumerateTest ( log ) );
  133.     ASSERT_RETURN_ZERO ( IterateTest ( log ) );
  134.     return true;
  135. }
  136.  
  137. /***********************************|****************************************/
  138.  
  139. Boolean
  140. InsertTest ( TDiskLog& log, TLogEntry& entry )
  141. {
  142.     unsigned long count = log.CountEntries ();
  143. //     ASSERT_RETURN_ZERO ( log.GetError () == noErr );
  144.     ASSERT_RETURN_ZERO ( log.AddEntry ( entry ) );
  145. //     ASSERT_RETURN_ZERO ( log.GetError () == noErr );
  146.     ASSERT_RETURN_ZERO ( EnumIterateTests ( log ) );
  147.     ASSERT_RETURN_ZERO ( count == log.CountEntries () - 1 );
  148. //     ASSERT_RETURN_ZERO ( log.GetError () == noErr );
  149.     ASSERT_RETURN_ZERO ( log.Close () );
  150. //     ASSERT_RETURN_ZERO ( log.GetError () == noErr );
  151.     ASSERT_RETURN_ZERO ( log.Open () );
  152. //     ASSERT_RETURN_ZERO ( log.GetError () == noErr );
  153.     ASSERT_RETURN_ZERO ( EnumIterateTests ( log ) );
  154.     TLogEntry* newEntry = log.CreateEntry ( count + 1 );
  155.     ASSERT_RETURN_ZERO ( EnumIterateTests ( log ) );
  156.     ASSERT_RETURN_ZERO ( newEntry != nil );
  157. //     ASSERT_RETURN_ZERO ( log.GetError () == noErr );
  158.     ASSERT_RETURN_ZERO ( entry == *newEntry );
  159.     ASSERT_RETURN_ZERO ( *newEntry == entry );
  160.     delete newEntry;
  161.     return true;
  162. }
  163.  
  164. /***********************************|****************************************/
  165.  
  166. void DiskLogTests ()
  167. {
  168.     TEST_REPORT(DiskLogTests);
  169.     
  170.     srand ( (unsigned int) clock () );
  171.     
  172.     TDiskLog log ( TFileSpec ( "disklog.test" ) );
  173.     ASSERT_RETURN ( log.Open ( TDiskLog::kRewrite ) );
  174.     
  175.     unsigned long count = 10;
  176.     
  177.     while ( count-- > 0 )
  178.     {
  179.         TTestEntry entry;     // create a random entry each time through the loop
  180.         
  181.         if ( !InsertTest ( log, entry ) ) 
  182.         {
  183.             chris << log << '\n';
  184.             return;
  185.         }
  186.     }
  187.     
  188.     ASSERT_RETURN ( log.Close () );
  189. }
  190.  
  191. /***********************************|****************************************/
  192.